//
//  TongJiCell.m
//  植物管理
//
//  Created by liujianlong on 2022/11/21.
//

#import "TongJiCell.h"
#import "RDayCell.h"

@interface TongJiCell()<XLCharViewDelegate>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSDateComponents *date;
@property (nonatomic, strong) NSArray *week;
///月份里第一天是星期几
@property (nonatomic, assign) int dayweek;
///一个月有多少天
@property (nonatomic, assign) int daymonth;
@property (strong, nonatomic) IBOutlet XLBarCharView *barchar;
@end
@implementation TongJiCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.week = @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];
    [self.collectionView registerNib:[RDayCell nib] forCellWithReuseIdentifier:[RDayCell ID]];
    self.barchar.showLastEntry = NO;
    self.barchar.delegate = self;
    self.barchar.charView.xAxis.labelCount = 12;
    self.barchar.charView.leftAxis.axisMinimum = 0;
    self.barchar.charView.leftAxis.axisMaximum = 50;
    self.barchar.charView.leftAxis.enabled = YES;
    self.barchar.charView.rightAxis.enabled = NO;
    self.barchar.charView.leftAxis.gridLineDashLengths = @[@(1),@(0)];
    self.barchar.charView.leftAxis.gridColor = [@"#3E6F51" color];
    self.barchar.charView.xAxis.labelWidth = 1;
    self.barchar.objcarray = @[@"0",@"3",@"0",@"0",@"0",@"0",@"6",@"0",@"0",@"0",@"0",@"0"];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
- (void)setStamp:(NSInteger)stamp{
    _stamp = stamp;
    self.daymonth = (int)[NSDate numOfMonth:self.stamp];
    
    NSString *string = [NSString stringWithFormat:@"%d/%d/1",(int)self.date.year, (int)self.date.month];
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    f.timeZone = [NSTimeZone localTimeZone];
    f.dateFormat = @"yyyy/MM/dd";
    NSDate *date = [f dateFromString:string];
    self.dayweek = (int)[NSDate weakDay:date];
    
    [self.collectionView reloadData];
    
    NSDateComponents *temp = [NSDate dateOfStamp:stamp];
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 1 ; i < 13; i++) {
        NSString *str = [NSString stringWithFormat:@"%d-%d-1",(int)temp.year,i];
        NSInteger stamp = [[NSDate tranToStamp:str:@"yyyy-MM-dd"] integerValue];
        NSInteger count = [PlantTool count:stamp complete:nil];
        [array addObject:[NSString stringWithFormat:@"%d",(int)count]];
    }
    self.barchar.objcarray = array;
    
}
- (NSDateComponents *)date{
    return [NSDate dateOfStamp:self.stamp];
}
#pragma mark - XLCharViewDelegate
- (void)chartValueNothingSelected:(ChartViewBase *)chartView{
    
}
- (void)chartValueSelected:(ChartViewBase *)chartView entry:(ChartDataEntry *)entry highlight:(ChartHighlight *)highlight{
    
}
- (ChartData *)chartDataSet:(NSArray<ChartDataEntryBase *> *)datas{
    NSArray *datas_new = [NSMutableArray arrayWithArray:datas];
    BarChartDataSet *set= [[BarChartDataSet alloc] initWithEntries:datas_new label:@""];
    [set setColor:[@"#C2E1CD" color]];
    set.drawIconsEnabled = NO;
    set.axisDependency = 0;
    BarChartData *data = [[BarChartData alloc] initWithDataSet:set];
    data.barWidth = 0.5;
    return data;
}
- (NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis{
    int month = (int)value + 1;
    return [NSString stringWithFormat:LS(@"%d"),month];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.week.count + self.dayweek - 1 + self.daymonth;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RDayCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[RDayCell ID] forIndexPath:indexPath];
    cell.content.text = @"";
    cell.point.hidden = YES;
    if (indexPath.row < 7) {
        cell.content.text = LS(self.week[indexPath.row]);
        cell.content.textHexColor = @"#333333";
    }else if (indexPath.row >= self.week.count + self.dayweek - 1){
        cell.content.text = [NSString stringWithFormat:@"%d",(int)(indexPath.row + 2 - self.week.count - self.dayweek)];
        cell.content.textHexColor = @"#333333";
    }
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < self.week.count + self.dayweek - 1) {
        return;
    }else{
        RDayCell *cell = (RDayCell *)[collectionView cellForItemAtIndexPath:indexPath];
        if (cell.point.hidden) {return;}
        //        NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@ and %@=%@ and %@=%@",bg_sqlKey(@"ID"),bg_sqlValue(APP.user.ID),bg_sqlKey(@"year"),bg_sqlValue(@(self.date.year)),bg_sqlKey(@"month"),bg_sqlValue(@(self.date.month)),bg_sqlKey(@"day"),bg_sqlValue(cell.content.text),bg_sqlKey(@"remove"),bg_sqlValue(@(NO))];
    }
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake((SCREEN_WIDTH - 40) / 7, 40);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
@end
